package com.geeklan.smart;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.geeklan.base.BaseApplication;
import com.geeklan.smart.data.auth.AuthManager;
import com.geeklan.smart.data.remote.rtc.GlobalSettings;
import com.geeklan.smart.umeng.UmengManager;
import com.geeklan.smart.utils.SharedPreferencesUtil;

import java.util.Set;

import me.leolin.shortcutbadger.ShortcutBadger;

public class MLMApplication extends BaseApplication {
    private static final String TAG = "mml";
    private static MLMApplication application;

    public static MLMApplication getApplication() {
        return application;
    }

    private static UmengManager umengManager;
    public static UmengManager getUmeng() {
        return umengManager;
    }
    private static boolean isForeground = false;
    private GlobalSettings globalSettings;
    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        AuthManager.init(this);
        SharedPreferencesUtil.init(this);
//        ShortcutBadger.applyCount(this, 10);

        umengManager = new UmengManager(this);
        umengManager.init();

        init(this);
    }

    private void init(Application application) {
        // 只有 Android 10 及以上时才使用 LifecycleObserver
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleObserver() {
                @OnLifecycleEvent(Lifecycle.Event.ON_START)
                public void onMoveToForeground() {
                    isForeground = true;
                }

                @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
                public void onMoveToBackground() {
                    isForeground = false;
                }
            });
        }
    }


    public boolean isAppInForeground(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            return isForeground;
        } else {
            return isAppForegroundLegacy(context);
        }
    }

    private boolean isAppForegroundLegacy(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager == null) return false;

        ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
        activityManager.getMyMemoryState(appProcessInfo);
        return appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
    }

    /**
     * Gets global settings.
     *
     * @return the global settings
     */
    public GlobalSettings getGlobalSettings() {
        if (globalSettings == null) {
            globalSettings = new GlobalSettings();
        }
        return globalSettings;
    }
}
